<?php


namespace App\Http\Controllers\Customer\Product;


use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Product\Index\StoreRequest;
use App\Http\Requests\Customer\Product\Index\UpdateRequest;
use App\Http\Requests\Customer\Product\Index\SimpleUpdateRequest;
use App\Http\Searchs\Customer\Product\IndexSearch;
use App\Repositories\Product;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * @var Product
     * @author luffyzhao@vip.126.com
     */
    private $product;

    /**
     * IndexController constructor.
     * @param Product $product
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Product $product)
    {
        $this->product = $product;
    }

    /**
     * 客户产品列表
     * @param IndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->product->simplePaginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->product->create($request->only([
                    'customer_id', 'sku', 'name', 'unit', 'condition', 'validity', 'lockup_validity', 'images'
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        return $this->response(
            $this->product->edit($id)
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($request, $id) {
                $this->product->update($id, $request->only([
                    'customer_id', 'sku', 'name', 'unit', 'condition', 'validity', 'lockup_validity', 'images'
                ]));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function affirm($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->product->affirm($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function lock($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->product->productLock($id);
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function unlock($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->product->unlock($id);
            })
        );
    }

    /**
     * 修改(确认之后只可以修改一些基本信息)
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function simple(SimpleUpdateRequest $request, $id){
        return $this->response(
            DB::transaction(function () use ($request, $id) {
                $this->product->simpleUpdate($id, $request->only([
                    'condition', 'validity', 'lockup_validity'
                ]));
            })
        );
    }
}
